package com.yhy.server.service;

import com.yhy.domain.UserInfo;
import com.yhy.domain.mongo.Comment;
import com.yhy.domain.vo.CommentVo;
import com.yhy.domain.vo.PageResult;
import com.yhy.dubbo.api.UserInfoApi;
import com.yhy.dubbo.api.mongo.CommentApi;
import com.yhy.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {

    @Reference
    private CommentApi commentApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //点赞
    public ResponseEntity like(String publishId) {
        //确定新增条件 当前用户的userId 此条评论的publishId和评论类型commentType 1
        Long userId = UserHolder.getUserId();

        //构建comment
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(1);

        //创建save方法 并返回保存后的数量
        Integer count = commentApi.save(comment);

        //将操作记录保留到redis
        redisTemplate.opsForValue().set("publish_comment_like_" + userId + "_" + publishId, "0");

        return ResponseEntity.ok(count);
    }

    //点赞-取消
    public ResponseEntity dislike(String publishId) {
        //确定删除条件 当前用户的userId 此条评论的publishId和评论类型commentType 1
        //1.获取用户Id
        Long userId = UserHolder.getUserId();

        //2.构建comment对象
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(1);

//        3.传递comment对象 返回删除后的点赞数
        Integer count = commentApi.delete(comment);

//        4.删除redis关于此动态的点赞记录
        redisTemplate.delete("publish_comment_like_" + userId + "_" + publishId);
        return ResponseEntity.ok(count);
    }

    //喜欢
    public ResponseEntity love(String publishId) {
        //确定新增需要的条件 当前用户userId 当前publishId 和 当前commentType 3
//        1.封装comment
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(3);
//        2.调用api新增 并返回新增后的loveCount
        Integer count = commentApi.save(comment);
//        3.redis添加本次新增comment的操作记录
        redisTemplate.opsForValue().set("publish_comment_love_" + userId + "_" + publishId, "0");
//        4.返回count
        return ResponseEntity.ok(count);
    }

    //喜欢-取消
    public ResponseEntity unlove(String publishId) {
        //确定删除需要的条件 当前用户userId 当前publishId 和 当前commentType 3
        //1.封装comment
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(3);
//        2.调用api新增comment 返回删除后的loveCount数
        Integer count = commentApi.delete(comment);
//        3.删除该条comment的操作记录
        redisTemplate.delete("publish_comment_love_" + userId + "_" + publishId);
//        4.返回count
        return ResponseEntity.ok(count);
    }

    //查找所有评论
    public ResponseEntity findAllComments(Integer page, Integer pagesize, String movementId) {

        PageResult pageResult = commentApi.queryCommentList(page, pagesize, movementId);

        List<Comment> items = (List<Comment>) pageResult.getItems();

        List<CommentVo> list = new ArrayList<>();
        for (Comment item : items) {
            UserInfo userInfo = userInfoApi.findById(item.getUserId());
            CommentVo commentVo = CommentVo.init(userInfo, item);
            if(redisTemplate.hasKey("public_comment_comment_like_"+userInfo.getId()+"_"+commentVo.getId())){
                commentVo.setHasLiked(1);
            }
            list.add(commentVo);
        }
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }


    //发布评论
    public ResponseEntity commitComment(String publishId, String content) {
        //确认新增需要的UserId 和commentType 2
        Long userId = UserHolder.getUserId();
//        1.构建comment
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setPublishId(new ObjectId(publishId));
        comment.setCreated(System.currentTimeMillis());
        comment.setContent(content);
        comment.setCommentType(2);

//        2.调用api添加comment
        commentApi.save(comment);
        return ResponseEntity.ok(null);
    }

    //评论点赞
    public ResponseEntity commentLike(String publishId) {
        System.out.println(publishId);
        //确认新增需要的userId commentType 4
        //1.封装comment
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(4);

        //2.调用API新增点赞 并返回新增后的likeCount
        Integer count = commentApi.save(comment);

//        3.把点赞的操作记录存redis
        redisTemplate.opsForValue().set("public_comment_comment_like_"+userId+"_"+publishId,"0");
        return ResponseEntity.ok(count);
    }

    //评论点赞-取消
    public ResponseEntity commentDislike(String publishId) {
        //确认删除需要的userId commentType 4
        //1.封装comment
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(4);

        //2.调用API删除点赞 并返回新增后的likeCount
        Integer count = commentApi.delete(comment);

        //3.redis删除点赞的操作记录
        redisTemplate.delete("public_comment_comment_like_"+userId+"_"+publishId);
        return ResponseEntity.ok(count);
    }
}